package com.zy.ipanalysis.controller;

import com.zy.ipanalysis.model.District;
import com.zy.ipanalysis.service.DistrictService;
import com.zy.commom.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by ZYDR on 2015/12/27.
 */
@Controller
@RequestMapping("district")
public class DistrictController {
    @Autowired
    DistrictService districtService;
    @RequestMapping(value = "/import")
    public String writeDistrict2Database(Model model){
        String[] provices={"北京市","广东省" ,"山东省","江苏省", "河南省", "上海市", "河北省","浙江省",
                "香港特别行政区","陕西省","湖南省","重庆市","福建省","天津市","云南省","四川省",
                "广西壮族自治区","安徽省","海南省","江西省","湖北省","山西省","辽宁省","台湾省",
                "黑龙江","内蒙古自治区","澳门特别行政区","贵州省","甘肃省","青海省","新疆维吾尔自治区",
                "西藏区","吉林省","宁夏回族自治区"};
        int i=0;
        try{
            if (districtService.count()>1){
                model.addAttribute("result","已有数据，不用重复添加");
                return "index";
            }
            while (i<provices.length){
                District district=new District();
                district.setProvince(provices[i]);
                districtService.insert(district);
                i++;
            }
            MailUtil.sendMail(MailUtil.DEFAULT_TARGET,MailUtil.SUCESS_TITLE,"操作代号：01<br>向t_district表中插入了"+i+"条数据<br>");
            model.addAttribute("result","插入"+i+"条数据");
        }
        catch(Exception e){
            model.addAttribute("result",e.getMessage());
            MailUtil.sendMail(MailUtil.DEFAULT_TARGET,MailUtil.FAILURE_TITLE,"操作代号：01<br>"+e.getMessage());
        }
        return "index";
    }
}
